package ubc.worker;

import java.io.File;
import java.util.Date;


public class Main {
   
    public static void main(String[] args) throws InterruptedException{

        int timeout = 30; // Default is 30 seconds
        int numConnections = 20;

        String serverAddr = new String();
        int serverPort = 0;

        long startTime = System.currentTimeMillis();
        Date startDate = new Date();
        Date endDate = new Date();
                
        // PARSE ARGUMENTS
        if ( (args.length == 0) || (args[0].equals("help")) ) {
            System.out.println("Usage: <serverHost:serverPort> <timeout=XX> <path=XX> numConcurrentConnections\n" +
                    "Default is <serverHost:serverPort> timeout=30 20");
            return;
        }
        else{
            for (int i=0; i<args.length; i++) {
                if(args[i].contains(":")) {
                    String[] hostport = args[i].split(":");
                    serverAddr = hostport[0];
                    serverPort = Integer.parseInt(hostport[1]);
                }
                if(args[i].startsWith("timeout")) {
                    timeout = Integer.parseInt(args[i].split("=")[1]);
                }
                if(args[i].startsWith("path")) {
                    ubc.Constants.clientFilePath = args[i].split("=")[1];
                }
                if(i == args.length-1) {
                    numConnections = Integer.parseInt(args[i]);
                }
            }

            if(serverAddr.isEmpty()) {
                System.out.println("Usage: <serverHost:serverPort> <timeout=XX> numConcurrentConnections\n" +
                    "Default is <serverHost:serverPort> timeout=30 20");
                return;
            }

            System.out.println("ARGUMENTS:");
            System.out.println("Server Address   : " + serverAddr + ":" + serverPort);
            System.out.println("Timeout          : " + timeout);
            System.out.println("NumConnections   : " + numConnections);
            System.out.println("Filepath         : " + ubc.Constants.clientFilePath );
            System.out.println("Absolute Filepath: " + (new File(ubc.Constants.clientFilePath)).getAbsolutePath() );
            System.out.println();
        }
        
        // START THE CRAWL MASTER
        try {
            Client mct = new Client(serverAddr+":"+serverPort, timeout, numConnections);
            mct.start();

            // Kill the Master once 'time' seconds have passed
            while (mct.isAlive()) {
                mct.join();
//                if (((System.currentTimeMillis() - startTime) > time*1000)) {
//                    System.out.println("Program timeout reached");
//                    endDate = new Date();
//                    mct.interrupt();
//                    mct.join();
//                } else {
//                    Thread.sleep(1000);
//                }
            }
        } catch ( Exception e) {
            e.printStackTrace();
        }
        
        System.out.println("Done!");
        System.exit(0);
            
    }   
}
